using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp.Data;
using Volo.Abp.Application.Dtos;
using Joy.Erp.BasicArchives.Permissions;
using Joy.Erp.BasicArchives.Core.Filters;

namespace Joy.Erp.BasicArchives.Receipt
{
    [Authorize]
    public class ReceivementTypeAppService : BasicArchivesAppService, IReceivementTypeAppService
    {
        protected IReceivementTypeRepository ReceivementTypeRepository => LazyServiceProvider.LazyGetRequiredService<IReceivementTypeRepository>();
        protected ReceivementTypeManager ReceivementTypeManager => LazyServiceProvider.LazyGetRequiredService<ReceivementTypeManager>();

        [Authorize(BasicArchivesPermissions.ReceivementType.Query)]
        public async Task<ReceivementTypeDto> GetAsync(Guid id)
        {
            return ObjectMapper.Map<ReceivementType, ReceivementTypeDto>(
                await ReceivementTypeRepository.GetAsync(id, true)
            );
        }

        [Authorize(BasicArchivesPermissions.ReceivementType.Query)]
        public async Task<ReceivementTypeDto> FindAsync(string filter)
        {
            var entity = await ReceivementTypeRepository.FindAsync(x => x.DisplayName == filter);
            return ObjectMapper.Map<ReceivementType, ReceivementTypeDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.ReceivementType.Query)]
        public async Task<ReceivementTypeDto> FindBySourceKeyAsync(string sourceKey, string sourceType)
        {
            var entity = await ReceivementTypeRepository.FindAsync(x => x.SourceKey == sourceKey && x.SourceType == sourceType);
            return ObjectMapper.Map<ReceivementType, ReceivementTypeDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.ReceivementType.Query)]
        public async Task<PagedResultDto<ReceivementTypeDto>> GetListAsync(FilterRequestDto input)
        {
            var totalCount = await ReceivementTypeRepository.GetCountAsync(input.Filter, true);
            var list = await ReceivementTypeRepository.GetPagedListAsync(input.Filter, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<ReceivementTypeDto>(
                totalCount,
                ObjectMapper.Map<List<ReceivementType>, List<ReceivementTypeDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.ReceivementType.Query)]
        public async Task<PagedResultDto<ReceivementTypeDto>> QueryAsync(FilterGroupRequestDto input)
        {
            Expression<Func<ReceivementType, bool>> expression = null;
            if (input != null && input.FilterGroup != null)
            {
                expression = FilterConverter.Convert<ReceivementType>(input.FilterGroup);
            }

            var totalCount = await ReceivementTypeRepository.GetCountAsync(expression, true);
            var list = await ReceivementTypeRepository.GetPagedListAsync(expression, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<ReceivementTypeDto>(
                totalCount,
                ObjectMapper.Map<List<ReceivementType>, List<ReceivementTypeDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.ReceivementType.Create)]
        public async Task<ReceivementTypeDto> CreateAsync(ReceivementTypeCreateDto input)
        {
            var entity = ObjectMapper.Map<ReceivementTypeCreateDto, ReceivementType>(input);

            await ReceivementTypeManager.CreateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize(BasicArchivesPermissions.ReceivementType.Update)]
        public async Task<ReceivementTypeDto> UpdateAsync(Guid id, ReceivementTypeUpdateDto input)
        {
            var entity = await ReceivementTypeRepository.GetAsync(id);
            entity.SetConcurrencyStampIfNotNull(input.ConcurrencyStamp);

            ObjectMapper.Map(input, entity);

            await ReceivementTypeManager.UpdateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize(BasicArchivesPermissions.ReceivementType.Delete)]
        public async Task DeleteAsync(Guid id)
        {
            await ReceivementTypeManager.DeleteAsync(id);
        }
    }
}